package com.kzd.sdk.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.ui.fragment.KZD_RebateApplyFragment;
import com.kzd.sdk.ui.fragment.KZD_RebateGuideFragment;
import com.kzd.sdk.ui.fragment.KZD_RebateSurfaceFragment;
import com.kzd.sdk.utils.AppUtils;

import java.util.LinkedList;
import java.util.List;

public class KZD_UserRebateActivity extends OwnerActivity {

    public static void open(Context context) {
        Intent intent = new Intent(context, KZD_UserRebateActivity.class);
        context.startActivity(intent);
    }

    private ViewPager vpUserRebateBody;
    private RadioGroup rgUserRebateGroup;
    private RadioButton rbUserRebateApply, rbUserRebateSurface, rbUserRebateGuide;

    @Override
    public String getLayoutIdByName() {
        return "kzd_activity_user_rebate";
    }

    @Override
    public View getLayoutView() {
        return null;
    }

    @Override
    public void initView() {

        findViewById(AppUtils.getId((this), "ivBackPressed"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });

        findViewById(AppUtils.getId((this), "ivUserRebateService"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_UserServiceActivity.open((KZD_UserRebateActivity.this));
                    }
                });

        findViewById(AppUtils.getId((this), "ivUserRebateRecord"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_RebateRecordActivity.open((KZD_UserRebateActivity.this));
                    }
                });


        vpUserRebateBody = findViewById(AppUtils.getId((this), ("vpUserRebateBody")));
        rgUserRebateGroup = findViewById(AppUtils.getId((this), ("rgUserRebateGroup")));
        rbUserRebateApply = findViewById(AppUtils.getId((this), ("rbUserRebateApply")));
        rbUserRebateSurface = findViewById(AppUtils.getId((this), ("rbUserRebateSurface")));
        rbUserRebateGuide = findViewById(AppUtils.getId((this), ("rbUserRebateGuide")));


        rgUserRebateGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                selectRadioToFragment(checkedId);
            }
        });

        vpUserRebateBody.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectFragmentToRadio(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initData() {

        //默认选中“申请返利”
        rbUserRebateApply.setChecked(true);

        List<Fragment> fragments = new LinkedList<>();
        fragments.add(new KZD_RebateApplyFragment());
        fragments.add(new KZD_RebateSurfaceFragment());
        fragments.add(new KZD_RebateGuideFragment());

        RebatePagerAdapter pagerAdapter = new RebatePagerAdapter(
                getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, fragments
        );
        vpUserRebateBody.setAdapter(pagerAdapter);
    }

    private void selectRadioToFragment(int checkedId) {
        if (checkedId == AppUtils.getId((this), ("rbUserRebateSurface"))) {
            vpUserRebateBody.setCurrentItem(1);
        } else if (checkedId == AppUtils.getId((this), ("rbUserRebateGuide"))) {
            vpUserRebateBody.setCurrentItem(2);
        } else {
            vpUserRebateBody.setCurrentItem(0);
        }
    }

    private void selectFragmentToRadio(int position) {
        if (position == 1) {
            rbUserRebateSurface.setChecked(true);
        } else if (position == 2) {
            rbUserRebateGuide.setChecked(true);
        } else {
            rbUserRebateApply.setChecked(true);
        }
    }

    private static class RebatePagerAdapter extends FragmentStatePagerAdapter {

        private final List<Fragment> fragments = new LinkedList<Fragment>();

        public RebatePagerAdapter(FragmentManager fm, int behavior, List<Fragment> fragments) {
            super(fm, behavior);
            this.fragments.clear();
            this.fragments.addAll(fragments);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

        }
    }
}
